package com.woniu.sharepark.common.service.impl;

import com.woniu.sharepark.common.JsonResult;
import com.woniu.sharepark.common.dao.UserInfoDAO;
import com.woniu.sharepark.common.pojo.Login;
import com.woniu.sharepark.common.pojo.UserInfo;
import com.woniu.sharepark.common.service.UserInfoService;
import com.woniu.sharepark.constant.SystemConstant;
import com.woniu.sharepark.leasesystem.common.dto.IDCardDTO;
import com.woniu.sharepark.leasesystem.common.dto.UserInfoDTO;
import com.woniu.sharepark.leasesystem.common.util.IDCardUtil;
import com.woniu.sharepark.leasesystem.tenant.dao.CarDAO;
import com.woniu.sharepark.util.DesUtil;
import com.woniu.sharepark.util.QRCodeUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author DELL
 * @date 2019/6/4 10:13
 * version v1.0
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoDAO userInfoDAO;

    @Resource
    private CarDAO carDAO;

    @Override
    public JsonResult addIDCard(IDCardDTO idCardDTO) {
        JsonResult jsonResult = IDCardUtil.idCardVerify(idCardDTO);
        if (jsonResult.getCode().equals(SystemConstant.ERROR_CODE)) {
            return jsonResult;
        }
        UserInfoDTO userInfoDTO = (UserInfoDTO) jsonResult.getResult();
        //获取当前用户
        UserInfo userInfo = (UserInfo) SecurityUtils.getSubject().getSession().getAttribute("currentUser");
        userInfo.setuName(userInfoDTO.getName());
        userInfo.setuCard(userInfoDTO.getCard());
        userInfoDAO.updateByPrimaryKeySelective(userInfo);
        return JsonResult.ok("身份信息添加成功");
    }

    @Override
    public JsonResult shareInviteUrl(HttpServletRequest request) {
        StringBuffer invitePath = new StringBuffer();
        invitePath.append("http://");
        String serverName = request.getServerName();
        if (serverName.equals("localhost")) {
            serverName = "127.0.0.1";
        }
        invitePath.append(serverName);
        invitePath.append(":" + request.getServerPort());
        invitePath.append("/sharepark/mregister.html");
        Session session = SecurityUtils.getSubject().getSession();
        Login login = (Login) session.getAttribute("currentUser");
        try {
            invitePath.append("?invite=" + DesUtil.encryptToBase64String(login.getUserInfo().getuId().toString(),"javaDesUtil"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map map = new HashMap();
        map.put("link",invitePath.toString());
        map.put("qrcode",QRCodeUtil.qrCodeCreat(invitePath.toString()));
        return new JsonResult("生成成功",SystemConstant.SUCCESS_CODE, map);
    }


    @Override
    public int updateUserInfo(UserInfo userInfo) {
        return userInfoDAO.updateByPrimaryKeySelective(userInfo);
    }

    @Override
    public UserInfo selectUserInfoById(Integer uId) {
        return userInfoDAO.selectByPrimaryKey(uId);
    }

    @Override
    public void getCredit(Login relLogin) {
        if (relLogin.getrId() == SystemConstant.TENANT_ROLE){
            UserInfo userInfo = relLogin.getUserInfo();
           userInfo.setuCredit(userInfoDAO.getCredit(userInfo.getuId()));
        }
    }


}
